package com.tony.study.designMode.DM4_原型模式;

import com.tony.study.designMode.DM4_原型模式.DM4.HomeWork;
import com.tony.study.designMode.DM4_原型模式.DM4.PupilStudent;
import com.tony.study.designMode.DM4_原型模式.DM4.SexEnum;
import com.tony.study.designMode.DM4_原型模式.DM4.WorkTypeEnum;
import com.tony.study.designMode.utils.DateUtils;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: PrototypeTest
 * @Auther: 皮蛋布丁
 * @Date: 2021/11/01/17:57
 * @Description: 原型模式测试（两种方式）
 */
public class PrototypeTest {

    public static void main(String[] args) throws CloneNotSupportedException {

        //原型 == 我们创建一个已经由小学生【张三】完成的作业对象
        HomeWork homeWork = new HomeWork();
        //设置作业信息
        homeWork.setType(WorkTypeEnum.WULI);
        homeWork.setPages(12);
        homeWork.setFinishTime(new Date());
        //设置小学生信息==张三
        PupilStudent pupilStudent = new PupilStudent();
        pupilStudent.setsNo(1001L);
        pupilStudent.setName("张三");
        pupilStudent.setAge(10);
        pupilStudent.setSex(SexEnum.FEMALE);
        pupilStudent.setsClass(4);
        homeWork.setPupilStudent(pupilStudent);

        //1、原型模式第一种 == 作业对象浅拷贝测试
        HomeWork myHomeWork = shallowCopy(homeWork);
        System.out.println("张三的作业：\n"+ homeWork);
        System.out.println("我的作业：\n"+ myHomeWork);

        System.out.println("====================分割线====================");

        //2、原型模式第二种 == 作业对象深拷贝测试
        HomeWork liHomeWork = deepCopy(myHomeWork);
        System.out.println("皮蛋布丁的作业为： \n" + myHomeWork);
        System.out.println("李四的作业为： \n" + liHomeWork);
    }

    /**
    * @Description: shallowCopy 对象浅拷贝
    * @Param: [homeWork]
    * @return: com.tony.study.designMode.MD4_原型模式.MD4.HomeWork
    * @Author: 皮蛋布丁
    * @Date: 2021/11/1 20:37
    */
    public static HomeWork shallowCopy(HomeWork homeWork) throws CloneNotSupportedException {

        /**
         * (1)复制一份【张三】的作业
         * (2)将复制过来的作业改成我自己的，个人信息必须改成自己的
         */

        HomeWork myHomeWork = homeWork.clone();

        //开始改造 == 首先改完成时间
        myHomeWork.setFinishTime(DateUtils.addDays(1));
        //然后改作业的完成者，就是我 == 皮蛋布丁
        PupilStudent myself = myHomeWork.getPupilStudent();
        //设置学号
        myself.setsNo(1002L);
        //设置名字
        myself.setName("皮蛋布丁");
        //性别
        myself.setSex(SexEnum.MALE);
        return myHomeWork;
    }

    /**
    * @Description: deepCopy 对象深度拷贝
    * @Param: [homeWork]
    * @return: com.tony.study.designMode.MD4_原型模式.MD4.HomeWork
    * @Author: 皮蛋布丁
    * @Date: 2021/11/1 20:50
    */
    public static HomeWork deepCopy(HomeWork homeWork) {

        HomeWork myHomeWork = homeWork.deepClone();
        //开始改造 == 首先改完成时间，完成时间+1天
        myHomeWork.setFinishTime(DateUtils.addDays(1));
        PupilStudent mySelf = myHomeWork.getPupilStudent();
        mySelf.setsNo(1003L);
        mySelf.setName("李四");
        mySelf.setSex(SexEnum.MALE);
        return myHomeWork;
    }


}
